﻿@model Jb.Web.ViewModels.JobViewModel
@using Jb.Common.Extensions;
@{
    //Layout = null;
    String pageTitle = Model == null ? "Job Details" : Model.Title;
    ViewBag.Title = Html.GetPageTitle(pageTitle);    
}
@functions{
    /// <summary>
    /// Creates a HTML bulleted list by separating the text with new line
    /// </summary>
    /// <param name="text"></param>
    /// <returns></returns>
    public String BuildBulletedList(String text, bool addUlTag = true)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder(10);
        if (!text.IsNullOrEmpty())
        {
            List<String> lines = new List<String>();
            if (text.IndexOf("\r\n") > -1)
                lines = text.Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToList();
            else if (text.IndexOf("\n") > -1)
                lines = text.Split(new String[] { "\n" }, StringSplitOptions.RemoveEmptyEntries).ToList();
            else
                lines = text.Split(new String[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList(); 
            if(lines.Count > 0)
            {
                if(addUlTag)
                    sb.Append("<ul>");
                foreach(String line in lines)
                {
                    if(!line.Trim().IsNullOrEmpty())
                        sb.AppendFormat("<li>{0}</li>", line.HtmlEncode());
                }
                if(addUlTag)
                    sb.Append("<ul>");
            }
            return sb.ToString();
        }
        return "N/A";//String.Empty;
    }
    /// <summary>
    /// Creates a HTML Bulleted List from the collection of generic objects
    /// </summary>
    /// <param name="list"></param>
    /// <returns></returns>
    public String BuildBulletedList(IList<Jb.Web.ViewModels.GenericViewModel> list)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder(10);
        if(list != null && list.Count > 0)
        {
            Jb.Web.ViewModels.GenericViewModel otherModel = list.FirstOrDefault(P => P.ID == null);
            if(otherModel != null)
                list.Remove(otherModel);
            sb.Append("<ul>");
            foreach(var item in list)
            {
                sb.AppendFormat("<li>{0}</li>", item.Name.HtmlEncode());
            }
            if(otherModel != null)
                sb.Append(BuildBulletedList(otherModel.Name, false));
            sb.Append("</ul>");
        }
        return sb.ToString();
    }
    
    public String BuildZGLProperties(Jb.Web.ViewModels.ZGL.Category.Category categoryZGL )
    {        
        if (categoryZGL != null)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder(10);
            foreach (Jb.Web.ViewModels.ZGL.Category.ProductGroup group in categoryZGL.Group)
            {
                //sb.Append("<div class=\"jb-group-title\">");
                //sb.Append("    <div class=\"float-left\">");
                   
                //String groupIcon = group.Icon.IsNullOrEmpty() ? "general.png" : group.Icon;
                //groupIcon = String.Format("{0}/{1}", AppConstants.Directories.PRODUCT_PROPERTY_GROUP_ICONS, groupIcon);
                //sb.AppendFormat("<img class=\"ItemGroupIcon\" src=\"{0}\" alt=\"\" title=\"{1}\" />", groupIcon, group.Name);
                //sb.Append("    </div>");
                //sb.AppendFormat("<div class=\"float-left ItemGroupTitle\">{0}</div>", group.Name);
                //sb.Append("    <div class=\"clear\"></div>");
                //sb.Append("</div>");
                sb.AppendFormat("<div class='jb-group-title'>{0}</div>", group.Name);                
                sb.Append("<ul>");
                foreach (Jb.Web.ViewModels.ZGL.Category.ProductGroupProperty property in group.Property)
                {
                    sb.Append(BuildBulletedList(String.Format("{0}: {1}", property.Label, property.Value.HtmlDecode()), false));
                    //<tr>
                    //    <td>@property.Label</td>
                    //    <td>@property.Value.HtmlDecode()</td>
                    //</tr>	        
                }
                sb.Append("</ul>");                
            }
            return sb.ToString();
        }
        return String.Empty;
    }  
}
@if(Model == null){
    <div class="jb-page-heading">
        <h1>@pageTitle</h1>
        <div>&nbsp;</div>
    </div>
    <div class="jb-page-content">@Html.ErrorMessageBox("Sorry! Requested Job was not found.")</div>
}
else 
{
    Layout = null;
    //All The Field Values will be replaced here
    System.Text.StringBuilder _Template = new System.Text.StringBuilder(WebUtil.ReadEmailTemplate(Model.JobTemplate.RelativePath));
    if (Model.JobTemplate.IsDefault)
    {
        _Template.Replace("[*PAGE_HEADER*]", Html.Partial("_Header").ToString());
        _Template.Replace("[*PAGE_FOOTER*]", Html.Partial("_Footer").ToString());
    }
    _Template.Replace("[*JOB_ID*]", Model.ID.ToString());
    _Template.Replace("[*JOB_TITLE*]", Model.Title.HtmlEncode());
    _Template.Replace("[*JOB_DESCRIPTION*]", Model.Description.FormatForHtml());
    _Template.Replace("[*POST_DATE*]", WebUtil.FormatData(Model.PostDate.ToDateTime(ConfigReader.CSharpCalendarDateFormat)));
    _Template.Replace("[*END_DATE*]", WebUtil.FormatData(Model.EndDate.ToDateTime(ConfigReader.CSharpCalendarDateFormat)));
    _Template.Replace("[*COMPANY*]", String.Format("<a href=\"/{0}\">{1}</a>", Model.Company.Url, Model.Company.Name.HtmlEncode()));    
    _Template.Replace("[*COMPANY_NAME*]", Model.Company.Name);
    _Template.Replace("[*COMPANY_LOGO*]", ImageManager.GetCompanyLogoUrl(Model.Company.LogoFile, Model.CompanyID));
    _Template.Replace("[*JOB_LEVEL*]", Model.JobLevel.Name.HtmlEncode());
    _Template.Replace("[*JOB_LOCATION*]", Model.JobLocation);
    _Template.Replace("[*EDUCATIONAL_REQUIREMENTS*]", BuildBulletedList(Model.SelectedEducations));
    _Template.Replace("[*JOB_REQUIREMENTS*]", BuildBulletedList(Model.SelectedSkills));
    _Template.Replace("[*SPECIAL_OFFER*]", BuildBulletedList(Model.SpecialOffer));
    _Template.Replace("[*SALARY_RANGE*]", Model.SalaryRangeFrom.HasValue && Model.SalaryRangeTo.HasValue ? String.Format("{1} {0} - {2} {0}", ConfigReader.CurrencyCode, WebUtil.FormatData(Model.SalaryRangeFrom), WebUtil.FormatData(Model.SalaryRangeTo)) : "Negotiable");
    _Template.Replace("[*ZGL_PROPERTIES*]", BuildZGLProperties(Model.CategoryZGL));
    @MvcHtmlString.Create(_Template.ToString());
        
}

@*<textarea style="width:100%; height:600px;">@Html.Partial("_Header")</textarea>*@